<template>
  <div class="header-box">
    <!-- 头部样式结构 -->
    <div class="top flex-four">
      <div class="top-left">
        <i class="el-icon-s-unfold" @click="expansion" v-if="isCollapse"></i>
        <i class="el-icon-s-fold" @click="expansion" v-else></i>
        <span>首页</span>
      </div>
      <div class="top-right flex-four">
        <img src="@/assets/logo.png" alt="" />
        <!-- 下拉选择 ui -->
        <div>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              用户名称<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <!--  -->
              <el-dropdown-item >退出登陆</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
    <!-- 路由记录:
     1vuex  路由导航 js ui
     -->
    <div class="bottom">
      <el-tag
        :key="tag.name"
        v-for="(tag,index) in routerDatas"
        :closable="routerDatas.length>1?true:false"
        :disable-transitions="false"
        @close="handleClose(tag,index)"
        @click="goRouter(tag)"
        :effect="tag.show ? 'dark' : 'light'"
      >
        {{ tag.name }}
      </el-tag>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  computed: {
    isCollapse: function () {
      //获取到vuexstate中的数据
      return this.$store.state.expansion;
    },
    // 获取vuex中state中的数据
    routerDatas: function () {
      return this.$store.state.routerData;
    },
  },
  methods: {
    expansion() {
      // 要是展开 false，或者是收缩 true,把vuex中的数据expansion修改
      if (this.$store.state.expansion) {
        this.$store.commit("changeEx", false);
      } else {
        this.$store.commit("changeEx", true);
      }
    },
    // 删除路由
    handleClose(tag,index) {
      // this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      this.$store.commit('deData',index)
      //[{name:首页，show:false,path:},]
      //获取路由记录数据
       if(tag.show){
        let path = this.routerDatas[this.routerDatas.length-1]
        this.$router.push(path.path)
       }
    },
    goRouter(tog) {
      this.$router.push(tog.path)
      this.$store.commit('changeActive',tog.path)
    },
    //退出登陆
    handleCommand(){
      // 1清除 sessionstorage
      sessionStorage.clear()
      //2回到外部页面
      this.$router.replace('/') //浏览器不会有记录
    }
  },
};
</script>

<style scoped lang="less">
.el-tag + .el-tag {
  margin-left: 10px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.header-box {
  padding: 12px 25px;
  // border-bottom: 1px solid  rgba(56, 55, 55,0.2);
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  border-radius: 20px;
  .top-left {
    span {
      margin-left: 16px;
      color: #606022;
    }
  }
  .top-right {
    img {
      height: 30px;
      width: 30px;
      border-radius: 50%;
      margin-right: 10px;
    }
  }
  .bottom{
    padding:8px 0;
    margin-top:10px;
    border-top:1px solid rgb(56, 55, 55);
  }
}
</style>